import axios from 'axios'
//请求拦截器 发起请求之前对请求参数进行处理

// Add a request interceptor
axios.interceptors.request.use(function (config) {
  // 判断本地的cookie中是否有token
  let token = window.sessionStorage.getItem("token");
  if (config.url == "/apis/stu/login") {
    return config;
  }
  if (token != null) {
    config.headers.Authorization = JSON.parse(token).sessionId;
  } else {
    // 跳转到登录页面(这里使用router，是因为路由文件引入到了此文件里)
    router.push('/login')
  }
  // Do something before request is sent
  return config;
}, function (error) {
  // Do something with request error
  return Promise.reject(error);
});

// 相应拦截器  接受到数据的时候数据进行处理
// Add a response interceptor
axios.interceptors.response.use(function (response) {
  // Any status code that lie within the range of 2xx cause this function to trigger
  // Do something with response data
  return response.data;
}, function (error) {
  // Any status codes that falls outside the range of 2xx cause this function to trigger
  // Do something with response error
  return Promise.reject(error);
});

export default axios